<?php

	// TITLE: Person AUTO MODEL
	// FILE: scaffolding/model/person.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createPerson = function($con, $person) {
		$sql = 'INSERT INTO person (id, address_id, title_id, first_name, last_name, status_id, gender, dob) VALUES (';
		$sql .= $person->getId() . ', ';
		$sql .= $person->getAddressId() . ', ';
		$sql .= $person->getTitleId() . ', ';
		$sql .= $person->getFirstName() . ', ';
		$sql .= $person->getLastName() . ', ';
		$sql .= $person->getStatusId() . ', ';
		$sql .= $person->getGender() . ', ';
		$sql .= $person->getDob() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readPerson = function($con, $id) {
		$sql = 'SELECT * FROM person WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$person = new Person();
			$person->setId(result[0]);
			$person->setAddressId($dbio->select($readAddress(result[1])));
			$person->setTitleId($dbio->select($readTitle(result[2])));
			$person->setFirstName(result[3]);
			$person->setLastName(result[4]);
			$person->setStatusId($dbio->select($readStatus(result[5])));
			$person->setGender(result[6]);
			$person->setDob(result[7]);
		} else {
			$person = false;
		}
		return $person;
	};

	$updatePerson = function($con, $person) {
		$sql = 'UPDATE person SET ';
		$sql .= 'id = ' . $person->getId() . ', ';
		$sql .= 'address_id = ' . $person->getAddressId() . ', ';
		$sql .= 'title_id = ' . $person->getTitleId() . ', ';
		$sql .= 'first_name = ' . $person->getFirstName() . ', ';
		$sql .= 'last_name = ' . $person->getLastName() . ', ';
		$sql .= 'status_id = ' . $person->getStatusId() . ', ';
		$sql .= 'gender = ' . $person->getGender() . ', ';
		$sql .= 'dob = ' . $person->getDob() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deletePerson = function($con, $id) {
		$sql = 'DELETE FROM person WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listPerson = function($con) {
		$sql = 'SELECT * FROM person';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$persons = array();
			while ($result = mysqli_fetch_array($results)) {
				$person = new Person();
				$person->setId(result[0]);
				$person->setAddressId($dbio->select($readAddress(result[1])));
				$person->setTitleId($dbio->select($readTitle(result[2])));
				$person->setFirstName(result[3]);
				$person->setLastName(result[4]);
				$person->setStatusId($dbio->select($readStatus(result[5])));
				$person->setGender(result[6]);
				$person->setDob(result[7]);
				$persons[] = person;
			}// end while
		} else {
			$persons = false;
		}
		return $persons;
	};

